// pages/active/active.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import formatDate from '../../utils/format_date';
import notice from '../../utils/notice.js';
import location from '../../utils/location.js';

let pageIndex = 1;
const citySelector = requirePlugin('citySelector');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: [{
                title: '推荐'
            }, {
                title: '本地'
            },
            /*{
                       title: '关注'
                   },*/
            {
                title: '周末'
            }
            // {
            //     imgTitle: {
            //         unselected: '/assets/weekend.png',
            //         selected: '/assets/weekend2.png'
            //     }
            // }
        ],
        tabs2: [
            {
                title: '热门'
            },
            {
                title: '全部'
            },
            {
                title: '读书会'
            },
            {
                title: '亲子活动',
                src: '/assets/qinzi.png'
            },
            {
                title: '瑜伽'
            },
            {
                title: '手工DIY'
            }
        ],
        currentTab: 0,
        statusBarHeight: 0,
        selectedIndex: 0,
        name: '武汉',
        list: [
            {
                id: 13141234,
                cover: [
                    'http://devhui.salonhui.com/assets/cover1.png'
                ],
                name: '知己共会·字鉴臻心，文创交流会，以书会友',
                activeDate: '05.24 周六 10:00~17:00',
                avatar: 'http://devhui.salonhui.com/assets/cover1.png',
                ownerName: '摇滚吧七零八零',
                status: '1',
                isVip: true,
                local: '武汉 · 绿地缤纷购物中心',
                distance: '420m',
                joinerAvatar: [
                    'http://devhui.salonhui.com/assets/cover1.png',
                    'http://devhui.salonhui.com/assets/cover2.png',
                    'http://devhui.salonhui.com/assets/cover1.png',
                    'http://devhui.salonhui.com/assets/cover2.png',
                    'http://devhui.salonhui.com/assets/cover1.png',
                    'http://devhui.salonhui.com/assets/cover1.png'
                ]
            },
            {
                id: 131412344444,
                cover: [
                    'http://devhui.salonhui.com/assets/cover2.png',
                    'http://devhui.salonhui.com/assets/cover1.png',
                    'http://devhui.salonhui.com/assets/cover2.png',
                ],
                avatar: 'http://devhui.salonhui.com/assets/cover2.png',
                ownerName: '饭团走天下',
                name: '遇见更好的自己-系列瑜伽公益课，快来报名,公益课，快来报名',
                activeDate: '2024-5-14',
                status: '2',
                local: '武汉 · 武昌万象城',
                distance: '1.2km',
                joinerAvatar: [
                    'http://devhui.salonhui.com/assets/cover1.png',
                    'http://devhui.salonhui.com/assets/cover2.png',
                    'http://devhui.salonhui.com/assets/cover1.png'
                ]

            },
            {
                id: 131412344444,
                cover: [
                    'http://devhui.salonhui.com/assets/cover2.png',
                    'http://devhui.salonhui.com/assets/cover1.png',
                    'http://devhui.salonhui.com/assets/cover2.png',
                    'http://devhui.salonhui.com/assets/cover1.png',
                    'http://devhui.salonhui.com/assets/cover2.png',
                ],
                avatar: 'http://devhui.salonhui.com/assets/cover2.png',
                isVip: true,
                ownerName: '不潮不花钱',
                status: '3',
                name: '拈花拾趣·漫享春光，创意插花手作 芬芳启幕',
                activeDate: '2024-5-13',
                local: '长沙 · 印象汇',
                distance: '2.2km',
                joinerAvatar: [
                    'http://devhui.salonhui.com/assets/cover1.png',
                ]
            },
            {
                id: 13141234,
                cover: [
                    'http://devhui.salonhui.com/assets/cover1.png'
                ],
                name: '拈花拾趣·漫享春光，创意插花手作 芬芳启幕',
                activeDate: '2024-5-12',
                avatar: 'http://devhui.salonhui.com/assets/cover2.png',
                ownerName: '摇滚吧七零八零',
                isVip: false,
                status: '3',
                local: '长沙 · 印象汇',
                distance: '2.2km'
            },
        ],
        isRefresh: false,
        address: '',
        city: ''
        // visible: false,
        // statusIndex: 0,
        // statusList: [{
        //     name: '全部',
        //     value: '1,2,3',
        //     selected: true
        // }, {
        //     name: '进行中',
        //     value: '1,2',
        //     selected: false
        // }, {
        //     name: '已结束',
        //     value: '3',
        //     selected: false
        // }]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.latitude = 30.49984;
        this.longitude = 114.34253;
        this.hasData = true;
        this.name = "";
        this.onRefresh();

        notice.addNotice("ActiveRefresh", this.onRefresh, this);
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: 0
            })
        }

        const selectedCity = citySelector.getCity();
        console.log(selectedCity, '已选择')
        let { name } = selectedCity
        this.setData({
            name
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        // const query = wx.createSelectorQuery().in(this);
        // query.select("#head").boundingClientRect((data) => {
        // let windowInfo = wx.getWindowInfo();
        // this.setData({
        //     height: windowInfo.safeArea.bottom - windowInfo.statusBarHeight - 44 - 48,
        //     statusBarHeight: windowInfo.statusBarHeight
        // })
        // }).exec();
    },
    stickyScroll(e) {
        if (e.detail.isFixed) {
            let windowInfo = wx.getWindowInfo();
            this.setData({
                statusBarHeight: windowInfo.statusBarHeight
            })
        } else if (this.data.statusBarHeight != 0) {
            this.setData({
                statusBarHeight: 0
            })
        }
    },
    switchTab (e) {
        let current = e.currentTarget.dataset.current
        this.setData({ currentTab: current  })
    },
    tabsChange(e) {
        this.setData({
            selectedIndex: e.detail
        })

        if (this.data.selectedIndex == 1) {
            location.getLocationAddress().then(res => {
                this.latitude = res.latitude;
                this.longitude = res.longitude;
                this.setData({
                    city: res
                });
                console.log(res)
                this.onRefresh();
            })
        } else {
            this.onRefresh();
        }
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        notice.removeNotice("ActiveRefresh", this);
    },

    onPullDownRefresh() {
        this.onRefresh();
    },

    onRefresh() {
        this.setData({
            isRefresh: true
        })
        pageIndex = 1;
        this.request();
    },

    onReachBottom() {
        if (this.hasData) {
            this.request();
        }
    },

    /**
     * 请求数据 
     * 将每一页的数据放一个数组，即形成一个二维数组,减少页面渲染的次数
     */
    request: function () {
        let param = {
            pageNo: pageIndex,
            pageSize: 20,
            onShelves: 1,
            queryType: this.data.selectedIndex
        }

        if (this.data.selectedIndex == 1) {
            param.cityName = this.data.city.city;
            param.latitude = this.latitude;
            param.longitude = this.longitude;
        }

        network.getJson(api.activePage, param).then(res => {
            this.setData({
                isRefresh: false
            })
            if (pageIndex == 1) {
                this.setData({
                    list: []
                })
            }
            let list = res.data.list;
            for (let i = 0; i < list.length; i++) {
                list[i].images = list[i].images.split(',');
                let time = list[i].bookingTimes;
                let len = time.length;
                if (len > 0) {
                    list[i].activeDate = formatDate.formatWeek4(time[0].startTime, time[len - 1].endTime);
                }

                if (list[i].users.length > 5) {
                    list[i].users = list[i].users.slice(-5);
                }
            }
            this.setData({
                list: this.data.list.concat(list)
            })
            if (this.data.list.length < res.data.total) {
                this.hasData = true;
                pageIndex += 1;
            } else {
                this.hasData = false;
            }
        })
    },

    chooseCity() {
        console.log(1231313)
        const key = 'PJIBZ-4WU3T-5RFXC-L7PPP-VGQVE-HDFVE'; // 使用在腾讯位置服务申请的key
        const referer = 'milehui'; // 调用插件的app的名称
        const hotCitys = '武汉,上海,北京,重庆'; // 用户自定义的的热门城市
        wx.navigateTo({
            url: `plugin://citySelector/index?key=${key}&referer=${referer}&hotCitys=${hotCitys}`,
          })
    },

    goActiveDetails(e) {
        wx.navigateTo({
            url: '/active/active-details/active-details?id=' + e.currentTarget.dataset.id
        })
    },

    goSearch() {
        wx.navigateTo({
            url: '/pages/search/search',
        })
    },

    // selectedTap(e) {
    //     let idx = e.currentTarget.dataset.index;
    //     for (let i = 0; i < this.data.statusList.length; i++) {
    //         this.data.statusList[i].selected = false;
    //     }
    //     this.data.statusList[idx].selected = true;
    //     this.setData({
    //         visible: false,
    //         statusList: this.data.statusList
    //     })
    //     this.data.statusIndex = idx;
    //     this.onRefresh();
    // }

})